#include"../include/cmd.h"

extern MYSQL* conn;
//初始化数据库
int DatabaseInit_server(void)
{
    //1.初始化连接语句
    char* host="localhost";
    char* user="root";
    char* passwd="1234";
    char* db="45th";
    conn=mysql_init(NULL);
    
    //2.建立连接
    if(mysql_real_connect(conn,host,user,passwd,db,0,NULL,0)==NULL)
    {
        printf("error:%s\n",mysql_error(conn));
        return EXIT_FAILURE;
    }
    else
    {
        printf("connect mysql succeed!\n");
        return 0;
    }
}

//打印表的内容
int showTable(char* tablename)
{
    //1.将select *查询语句存入query中
    char query[2048]={0};
    sprintf(query,"select * from %s",tablename);
    
    //2.向数据库发送查询语句
    int ret=mysql_query(conn,(const char*)query);
    if(ret!=0)
    {
        printf("error query1:%s\n",mysql_error(conn));
        return EXIT_FAILURE;
    }
    
    //3.获取查询结果
    MYSQL_RES* result=mysql_store_result(conn);
    if(result==NULL)
    {
        printf("error store_result:%s\n",mysql_error(conn));
        return EXIT_FAILURE;
    }
    
    //4.获取查询结果的行数和列数
    int rows=mysql_num_rows(result);        //行数 用户数
    int cols=mysql_num_fields(result);      //列数 信息数
    printf("\nTable name:%s  >>rows:%d  cols:%d\n",tablename,rows,cols);
    printf(">>-----------------------------------------------------<<\n");

    //5.打印每一行信息
    MYSQL_ROW row;
    while((row=mysql_fetch_row(result))!=NULL)
    {
        for(int i=0;i<cols;i++)
        {
            printf("%10s ",row[i]);
        }
        printf("\n");
    }
    printf(">>----------------------------------------------------<<\n");

    //释放结果
    mysql_free_result(result);
}


int mysqlRegisterUsername(int peerfd,char*regist_name)
{
    MYSQL_RES* result;
    MYSQL_ROW row;
    char query[2046]={0};
    char buf[4096]={0};
    int ret=0;
    
    //接收来自客户端的名字
    char registname[66]={0};
    int registnamelen=0;
    recv(peerfd,&registnamelen,4,MSG_WAITALL);
    recv(peerfd,registname,registnamelen,MSG_WAITALL);

    //根据收到的名字判重
    sprintf(query,"%s%s%s%s","select username from user where username=",
                                "'",registname,"'");

    //将语句发送给数据库查询
    ret=mysql_query(conn,query);
    if(ret!=0)
    {
        printf("error query1:%s\n",mysql_error(conn));
        return EXIT_FAILURE;
    }
    
    //接收查询结果
    result=mysql_use_result(conn);
    row=mysql_fetch_row(result);
    if(row==NULL)
    {
        int ret=1;
        send(peerfd,&ret,4,MSG_WAITALL);  //成功返回1
        strcpy(regist_name,registname);
        mysql_free_result(result);
        return 1;
    }
    ret=0;
    send(peerfd,&ret,4,MSG_WAITALL);      //失败返回0
    regist_name=NULL;
    mysql_free_result(result);
    return 0;
}

int mysqlRegisterPasswd(int peerfd,const char* registusername)
{
    MYSQL_RES* result;
    MYSQL_ROW row;
    char query[100]={0};
    char buf[4096]={0};
    char* cryptpasswd;
    int id=0;
    int ret=0;
    char clientpasswd[66]={0};
    int length=0;
    //接收来自客户端的密码
    recv(peerfd,&length,4,MSG_WAITALL);
    recv(peerfd,clientpasswd,length,MSG_WAITALL);
    
    //获取随机盐值
    char salt[13]={0};
    getrandomsalt(salt);
    printf("salt=%s\n",salt);

    //获取密文
    cryptpasswd=crypt(clientpasswd,salt);  //给密码加密
    
    //制作语句
    sprintf(query,"%s%s%s%s%s%s%s%s%s%s%s%s%s%s",
                "insert into user(username,salt,cryptpasswd,pwd)value(",
                "'",
                registusername,
                "'",
                ",",
                "'",
                salt,
                "'",
                ",",
                "'",
                cryptpasswd,
                "'",
                ",",
                "'/');");

    ret=mysql_query(conn,query);
    if(ret!=0)
    {
        printf("error query1:%s\n",mysql_error(conn));
        return EXIT_FAILURE;
    }

    //验证是否注册进入数据库
    memset(&query,0,sizeof(query));
    sprintf(query,"%s%s%s","select id from user where username='",registusername,"';");
    ret=mysql_query(conn,query);
    if(ret!=0)
    {
        printf("error query1:%s\n",mysql_error(conn));
        return EXIT_FAILURE;
    }

    result=mysql_use_result(conn);
    row=mysql_fetch_row(result);
    id=atoi(row[0]);
    printf("registname id=%d\n",id);
    mysql_free_result(result);
}

